
window.addEventListener("load", function () {
    setTimeout("playBGM()", 2000);
});

var about = function () {
    alert("Robot Finds Kitty 3D -- By Mingkai");
    alert("This prog is a Intel Summer Porject");
    alert("You can try the following keys:\n<w><a><s><d><f><g><4><5><r><t>\n & <UP><DOWN><LEFT><RIGHT>\n <;><'><,><.><-><=><[><]>");
};

//{{{
$(document).keydown(function(event){
    console.log("keydown");
    console.log(event.which);

    var robot0, robot1, crv0, crv1;
    robot0 = robots[0];
    crv0 = cam_rel_vecs[0];
    if (player_num > 1){
        robot1 = robots[1];
        crv1 = cam_rel_vecs[1];
    }else{
        robot1 = robot0;
        crv1 = crv0;
    }

    switch (event.which){
//player 1
    case 37://Left
        robot1.rot.z = 0.05;
        robot1.pos = rotate(robot1.pos, robot1.rot.z);
        break;
    case 38://Up
        robot1.pos = rotate(ROBOT_FORE, robot1.dae.rotation.z)
                        .setLength(WALK_STEP*robot1.speed);
        break;
    case 39://Right
        robot1.rot.z = -0.05;
        robot1.pos = rotate(robot1.pos, robot1.rot.z);
        break;
    case 40://Down
        robot1.pos = rotate(ROBOT_FORE, robot1.dae.rotation.z)
                        .setLength(WALK_STEP*robot1.speed).negate();
        break;
    case 188://","CameraLeft
        crv1.copy(rotate(crv1, -0.1));
        break;
    case 190://"."CameraRight
        crv1.copy(rotate(crv1, 0.1));
        break;
    case 189://"-"Zoom out
        var len = crv1.length()+0.05;
        crv1.setLength(len);
        break;
    case 187://"="Zoom in
        var len = crv1.length()-0.05;
        crv1.setLength(len);
        break;
    case 219://"["CameraUp
        var len = crv1.length();
        crv1.z += 0.25;
        //vec.setLength(len0);
        break;
    case 221://"]"CameraDown
        var len = crv1.length();
        crv1.z -= 0.25;
        //vec.setLength(len0);
        break;
    case 222://"'"shoot
        if (!robot1.can_shoot){
            break;
        }
        var direction = rotate(ROBOT_FORE, robot1.dae.rotation.z);
        
        var pos = (new THREE.Vector3(0,0,0))
                    .add(direction.setLength(0.8), robot1.dae.position);
        console.log(pos.x, pos.y, pos.z);

        var idx = loadCollada(ObjType.BALL, 
                    MODEL_FILES[ObjType.BALL], pos.x, pos.y, pos.z);

        console.log(idx);
        balls[idx].pos = direction.setLength(0.25);
        playAudioEffect("fire");
        break;
    case 186://";"jump
        if (robot1.jumped < robot1.max_jump){
			robot1.jumping = JUMP_HEIGHT;
            robot1.dae.position.z += JUMP_STEP;
			++robot1.jumped;
            playAudioEffect("jump");
		}
        break;


//player 2
    case 65://"a"Left
        robot0.rot.z = 0.05;
        robot0.pos = rotate(robot0.pos, robot0.rot.z);
        break;
    case 87://"w"Up
        robot0.pos = rotate(ROBOT_FORE, robot0.dae.rotation.z)
                        .setLength(WALK_STEP*robot0.speed);
        break;
    case 68://"d"Right
        robot0.rot.z = -0.05;
        robot0.pos = rotate(robot0.pos, robot0.rot.z);
        break;
    case 83://"s"Down
        robot0.pos = rotate(ROBOT_FORE, robot0.dae.rotation.z)
                        .setLength(WALK_STEP*robot0.speed).negate();
        break;

    case 70://"f"CameraLeft
        crv0.copy(rotate(crv0, -0.1));
        break;
    case 71://"g"CameraRight
        crv0.copy(rotate(crv0, 0.1));
        break;
    case 52://"4"Zoom out
        var len = crv0.length()+0.05;
        crv0.setLength(len);
        break;
    case 53://"5"Zoom in
        var len = crv0.length()-0.05;
        crv0.setLength(len);
        break;
    case 82://"r"CameraUp
        var len = crv0.length();
        crv0.z += 0.25;
        //vec.setLength(len0);
        break;
    case 84://"t"CameraDown
        var len = crv0.length();
        crv0.z -= 0.25;
        //vec.setLength(len0);
        break;
    case 86://"v"shoot
        if (!robot0.can_shoot){
            break;
        }
        var direction = rotate(ROBOT_FORE, robot0.dae.rotation.z);
        
        var pos = (new THREE.Vector3(0,0,0))
                    .add(direction.setLength(0.8), robot0.dae.position);
        console.log(pos.x, pos.y, pos.z);
        var idx = loadCollada(ObjType.BALL,
                    MODEL_FILES[ObjType.BALL], pos.x, pos.y, pos.z);
        console.log(idx);
        balls[idx].pos = direction.setLength(0.1);
        playAudioEffect("fire");
        break;
    case 67://"c"jump
        console.log(robot0.jumped);
		if (robot0.jumped < robot0.max_jump){
	        robot0.jumping = JUMP_HEIGHT;
            robot0.dae.position.z += JUMP_STEP;
			++robot0.jumped;
            playAudioEffect("jump");
		}
        console.log(robot0.jumped);
        break;

    default:
        break;
    }
});

//}}}

//{{{
$(document).keyup(function(event){
    var robot0, robot1;
    robot0 = robots[0];
    if (player_num > 1) robot1=robots[1]; else robot1=robot0;
    
    switch (event.which){
    case 37://Left
        robot1.rot.z = 0;
        break;
    case 38://Up
        robot1.pos.set(0, 0, 0);
        break;
    case 39://Right
        robot1.rot.z = 0;
        break;
    case 40://Down
        robot1.pos.set(0, 0, 0);
        break;

    case 65://"a"Left
        robot0.rot.z = 0;
        break;
    case 87://"w"Up
        robot0.pos.set(0,0,0);
        break;
    case 68://"d"Right
        robot0.rot.z = 0;
        break;
    case 83://"s"Down
        robot0.pos.set(0,0,0);
        break;

    default:
        break;
    }
});
//}}}


